﻿using HDW_Migrate.Migrate;
using HDW_Migrate.Resources;
using Microsoft.AspNetCore.Mvc;
using static HDW_Migrate.Migrate.MigrateHelper;

namespace HDW_Migrate.Controllers;

/// <summary>
/// 数据迁移
/// </summary>
/// <param name="logger"></param>
[ApiController]
[Route("[controller]")]
public class MigrateController(ILogger<MigrateController> logger)
{
    /// <summary>
    /// 获取 RIS 数据库中订单的最大和最小注册时间
    /// </summary>
    /// <returns></returns>
    [HttpGet("time")]
    public object GetTime()
    {
        var time = MigrateHelper.GetTime();
        return new { min = time.Item1, max = time.Item2 };
    }

    /// <summary>
    /// 迁移数据
    /// </summary>
    /// <returns></returns>
    [HttpPost("migrate")]
    public string Migrate(MigrateParam param)
    {
        if (Running) return "数据正在迁移，请稍后再试";
        Running = true;
        try
        {
            MigrateData(param);
        }
        catch (Exception) { throw; }
        finally
        {
            Running = false;
        }
        return "数据迁移完成。";
    }

    /// <summary>
    /// 获取最后一次迁移的结果
    /// </summary>
    /// <returns></returns>
    [HttpGet("result")]
    public MigrateResult GetResult()
    {
        if (Running) return new MigrateResult { Msg = "正在迁移，请稍后再试" };
        return Result;
    }

}
